<?php require_once('connections/con_gl.php'); ?>

<?php

cekAkses($_SESSION[akses],"KR-1-3");

mysql_select_db($database_con_gl, $con_gl);

$query_kontak = "SELECT id, nama, type, tcustomer FROM gl_kontak where gl_kontak.type ='Customer' ORDER BY type, nama ASC";

$kontak = mysql_query($query_kontak, $con_gl) or die(mysql_error());

$row_kontak = mysql_fetch_assoc($kontak);

$totalRows_kontak = mysql_num_rows($kontak);



// ---

mysql_select_db($database_con_gl, $con_gl);

$query_gudang = "SELECT id, gudang FROM gl_gudang";

if(!strstr($_SESSION[akses],'A-1-4')) {

	$query_gudang .= " where id='$_SESSION[lokasi]'";

}

$query_gudang .=" ORDER BY gudang ASC";

$gudang = mysql_query($query_gudang, $con_gl) or die(mysql_error());

$row_gudang = mysql_fetch_assoc($gudang);

$totalRows_gudang = mysql_num_rows($gudang);



mysql_select_db($database_con_gl, $con_gl);

$query_satuan = "SELECT id, satuan FROM gl_satuan ORDER BY satuan ASC";

$satuan = mysql_query($query_satuan, $con_gl) or die(mysql_error());

$row_satuan = mysql_fetch_assoc($satuan);

$totalRows_satuan = mysql_num_rows($satuan);



mysql_select_db($database_con_gl, $con_gl);

$query_kelom = "SELECT id, kelompok FROM gl_kelompok ORDER BY kelompok ASC";

$kelom = mysql_query($query_kelom, $con_gl) or die(mysql_error());

$row_kelom = mysql_fetch_assoc($kelom);

$totalRows_kelom = mysql_num_rows($kelom);



mysql_select_db($database_con_gl, $con_gl);

$query_klas = "SELECT kd, klasifikasi, keterangan, neraca, ruglab, tipe FROM gl_klas where tipe = 2 ORDER BY kd,klasifikasi ASC";

$klas = mysql_query($query_klas, $con_gl) or die(mysql_error());

$row_klas = mysql_fetch_assoc($klas);

$totalRows_klas = mysql_num_rows($klas);



mysql_select_db($database_con_gl, $con_gl);

$query_def_ak = "SELECT gl_company.a_kas FROM gl_company";

$def_ak = mysql_query($query_def_ak, $con_gl) or die(mysql_error());

$row_def_ak = mysql_fetch_assoc($def_ak);

$totalRows_def_ak = mysql_num_rows($def_ak);


mysql_select_db($database_con_gl, $con_gl);

$query_edit = "select * from gl_kontrak where no='$_GET[no]'";

$edit = mysql_query($query_edit, $con_gl) or die(mysql_error());

$row_edit = mysql_fetch_assoc($edit);

$totalRows_edit = mysql_num_rows($edit);



mysql_select_db($database_con_gl, $con_gl);

$query_getidh = "SELECT gl_trans.id, gl_trans.akun, gl_trans.total FROM gl_trans WHERE gl_trans.no_ref='$_REQUEST[noreferensi]' AND gl_trans.transaksi LIKE '%%Piutang%%'";

$getidh = mysql_query($query_getidh, $con_gl) or die(mysql_error());

$row_getidh = mysql_fetch_assoc($getidh);

$totalRows_getidh = mysql_num_rows($getidh);



mysql_select_db($database_con_gl, $con_gl);

$query_getidh2 = "SELECT gl_trans.id, gl_trans.total FROM gl_trans WHERE gl_trans.no_ref='$_REQUEST[noreferensi]' AND gl_trans.transaksi LIKE '%%Pembelian%%'";

$getidh2 = mysql_query($query_getidh2, $con_gl) or die(mysql_error());

$row_getidh2 = mysql_fetch_assoc($getidh2);

$totalRows_getidh2 = mysql_num_rows($getidh2);



// --- list kendaraan
mysql_select_db($database_con_gl, $con_gl);
$query_lkend = "select a.id, a.kendaraan as nopol, a.harga, a.diskon, a.total, a.transaksi, b.tipe, c.jenis from gl_dkontrak a, gl_kendaraan b, gl_jkendaraan c where a.kontrak='$_GET[no]' AND a.kendaraan = b.nopol AND b.jenis = c.id order by a.id";
$lkend = mysql_query($query_lkend, $con_gl) or die(mysql_error());
$row_lkend = mysql_fetch_assoc($lkend);
$total_lkend = mysql_num_rows($lkend);


mysql_select_db($database_con_gl, $con_gl);

$query_getbiaya = "SELECT distinct gl_rtrans.akbiaya, gl_trans.id, gl_trans.total FROM gl_rtrans, gl_trans WHERE gl_trans.no_ref='$_GET[noreferensi]' AND gl_trans.akun=gl_rtrans.akbiaya";

$getbiaya = mysql_query($query_getbiaya, $con_gl) or die(mysql_error());

$row_getbiaya = mysql_fetch_assoc($getbiaya);

$totalRows_getbiaya = mysql_num_rows($getbiaya);



mysql_select_db($database_con_gl, $con_gl);

$query_getpajak = "SELECT distinct gl_rtrans.akpjk, gl_trans.id, gl_trans.total FROM gl_rtrans, gl_trans WHERE gl_trans.no_ref='$_GET[noreferensi]' AND gl_trans.akun=gl_rtrans.akpjk";

$getpajak = mysql_query($query_getpajak, $con_gl) or die(mysql_error());

$row_getpajak = mysql_fetch_assoc($getpajak);

$totalRows_getpajak = mysql_num_rows($getpajak);



mysql_select_db($database_con_gl, $con_gl);

$query_getbyr = "SELECT gl_trans.id, gl_trans.total, gl_trans.akun FROM gl_trans WHERE gl_trans.no_ref='$_GET[noreferensi]' AND gl_trans.transaksi LIKE'%%Pembayaran%%'";

$getbyr = mysql_query($query_getbyr, $con_gl) or die(mysql_error());

$row_getbyr = mysql_fetch_assoc($getbyr);

$totalRows_getbyr = mysql_num_rows($getbyr);



mysql_select_db($database_con_gl, $con_gl);

$query_klashut = "SELECT kd, klasifikasi, keterangan, neraca, ruglab, tipe FROM gl_klas where tipe = 6 ORDER BY kd,klasifikasi ASC";

$klashut = mysql_query($query_klashut, $con_gl) or die(mysql_error());

$row_klashut = mysql_fetch_assoc($klashut);

$totalRows_klashut = mysql_num_rows($klashut);



mysql_select_db($database_con_gl, $con_gl);

$query_salesmen = "SELECT gl_kontak.id, gl_kontak.nama FROM gl_kontak WHERE gl_kontak.type='Karyawan' AND tkaryawan='sales' order by nama";

$salesmen = mysql_query($query_salesmen, $con_gl) or die(mysql_error());

$row_salesmen = mysql_fetch_assoc($salesmen);

$totalRows_salesmen = mysql_num_rows($salesmen);



mysql_select_db($database_con_gl, $con_gl);

$query_getkakun = "SELECT gl_trans.akun, gl_klas.klasifikasi, gl_akun.klasifikasi as klas_id FROM gl_trans, gl_klas, gl_akun WHERE gl_trans.no_ref='$_GET[noreferensi]' AND gl_trans.pos='K' AND gl_klas.tipe=2 AND gl_akun.klasifikasi = gl_klas.kd AND gl_trans.akun = gl_akun.id";

$getkakun = mysql_query($query_getkakun, $con_gl) or die(mysql_error());

$row_getkakun = mysql_fetch_assoc($getkakun);

$totalRows_getkakun = mysql_num_rows($getkakun);



mysql_select_db($database_con_gl, $con_gl);

$query_gethutang = "SELECT distinct gl_rtrans.akpjk, gl_trans.id, gl_trans.total FROM gl_rtrans, gl_trans WHERE gl_trans.no_ref='$_GET[noreferensi]' AND gl_trans.akun='$row_getidh[akun]'";

$gethutang = mysql_query($query_gethutang, $con_gl) or die(mysql_error());

$row_gethutang = mysql_fetch_assoc($gethutang);

$totalRows_gethutang = mysql_num_rows($gethutang);



mysql_select_db($database_con_gl, $con_gl);

$query_klasbeban = "SELECT * FROM gl_klas WHERE gl_klas.tipe=10";

$klasbeban = mysql_query($query_klasbeban, $con_gl) or die(mysql_error());

$row_klasbeban = mysql_fetch_assoc($klasbeban);

$totalRows_klasbeban = mysql_num_rows($klasbeban);



mysql_select_db($database_con_gl, $con_gl);

$query_klasbeban2 = "SELECT * FROM gl_klas WHERE gl_klas.tipe=9";

$klasbeban2 = mysql_query($query_klasbeban2, $con_gl) or die(mysql_error());

$row_klasbeban2 = mysql_fetch_assoc($klasbeban2);

$totalRows_klasbeban2 = mysql_num_rows($klasbeban2);



$totdeb = 0;

$totkre = 0;

?>

<link rel="stylesheet" type="text/css" href="css/ui.datepicker.css"/>
<style>
#cusinfo tr td {
	font-size:10px;	
}
</style>
<script type="text/javascript" src="js/ajax_data.js"></script>

<script type="text/javascript" src="js/ui.datepicker.js"></script>

<script type="text/javascript">

$(function()

      {

        $('.calendar').datepicker({

            appendText : "",

            dateFormat : 'yy/mm/dd'

          });

      });

</script>

<script type="text/javascript">

 $(document).ready(function() {



	$().ajaxStart(function() {

		$('#loading').show();

		$('#result').hide();

	}).ajaxStop(function() {

		$('#loading').hide();

		$('#result').fadeIn('slow');

	});



	$('#edit').submit(function() {

		$.ajax({

			type: 'POST',

			url: $(this).attr('action'),

			data: $(this).serialize(),

			success: function(data) {

				$('#result').html(data);

			}

		})

		return false;

	});

  $('#result').click(function(){

  $(this).hide();

  });

})

</script>

<div id="loading" style="display:none;"><img src="images/loading.gif" alt="loading..." /></div>

<div id="result" style="display:none;"></div>

<h1>Hapus Kontrak</h1>
<div id="divDetail">
  <table width="100%" border="0" cellspacing="0" cellpadding="4" class="datatable" id="cusinfo">
    <tr>
      <th colspan="2" align="center">Customer Info</th>
    </tr>
    <tr>
      <td align="right" valign="top" style="background:#FFF;">NPWP :</td>
      <td valign="top" style="background:#FFF;">&nbsp;</td>
    </tr>
    <tr>
      <td width="20%" align="right" valign="top" style="background:#FFF;">Nama :</td>
      <td valign="top" style="background:#FFF;">&nbsp;</td>
    </tr>
    <tr>
      <td align="right" valign="top" style="background:#FFF;">Alamat :</td>
      <td valign="top" style="background:#FFF;">&nbsp;</td>
    </tr>
    <tr>
      <td align="right" valign="top" style="background:#FFF;">No. Tlp :</td>
      <td valign="top" style="background:#FFF;">&nbsp;</td>
    </tr>
    <tr>
      <td align="right" valign="top" style="background:#FFF;">Email :</td>
      <td valign="top" style="background:#FFF;">&nbsp;</td>
    </tr>
    <tr>
      <td align="right" valign="top" style="background:#FFF;">Keterangan :</td>
      <td valign="top" style="background:#FFF;">&nbsp;</td>
    </tr>
  </table>
</div>
<form action="proses/kontrak.php?act=delete" method="post" name="edit" id="edit">
  <table width="98%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable">
    <tr valign="top">
      <td width="14%" align="right">No. Kontrak :</td>
      <td width="32%"><div id="refDiv">
        <label>
          <input name="noreferensi" type="text" id="noreferensi" value="<?php echo $row_edit['no'];?>" size="25" maxlength="100" readonly="readonly"/>
        </label>
      </div></td>
      <td width="12%" align="right">Lokasi Kantor :</td>
      <td><select name="gudang" id="gudang">
        <?php

do {  

?>
        <option value="<?php echo $row_gudang['id']?>" <?php if($row_gudang['id']==$row_edit['gudang']) { ?>selected="selected"<?php } ?>><?php echo $row_gudang['gudang']?></option>
        <?php

} while ($row_gudang = mysql_fetch_assoc($gudang));

  $rows = mysql_num_rows($gudang);

  if($rows > 0) {

      mysql_data_seek($gudang, 0);

	  $row_gudang = mysql_fetch_assoc($gudang);

  }

?>
      </select></td>
    </tr>
    <tr>
      <td align="right" valign="top">Tanggal :</td>
      <td valign="top"><label>
        <input name="tanggal" type="text" class="calendar" id="tanggal" value="<?php echo $row_edit['tgl'];?>" size="20" maxlength="15"/>
      </label></td>
      <td align="right" valign="top">Durasi Kontrak :</td>
      <td valign="top"><label>
        <select name="durasi" id="durasi" onchange="genAkhirKontrak();">
          <option value="" <?php if (!(strcmp("", $row_edit['durasi']))) {echo "selected=\"selected\"";} ?>>Durasi Kontrak</option>
          <option value="12" <?php if (!(strcmp(12, $row_edit['durasi']))) {echo "selected=\"selected\"";} ?>>1 Tahun</option>
          <option value="24" <?php if (!(strcmp(24, $row_edit['durasi']))) {echo "selected=\"selected\"";} ?>>2 Tahun</option>
          <option value="36" <?php if (!(strcmp(36, $row_edit['durasi']))) {echo "selected=\"selected\"";} ?>>3 Tahun</option>
          <option value="48" <?php if (!(strcmp(48, $row_edit['durasi']))) {echo "selected=\"selected\"";} ?>>4 Tahun</option>
          <option value="60" <?php if (!(strcmp(60, $row_edit['durasi']))) {echo "selected=\"selected\"";} ?>>5 Tahun</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td align="right" valign="top">Customer :</td>
      <td valign="top"><!-- Added by suwondo -->
	  <input type="hidden" name="dari" id="dari" value="<?php echo $row_edit['kontak'];?>" />
	  <?php list($namakontak) = mysql_fetch_row(mysql_query("select nama from gl_kontak where id = '$row_edit[kontak]'")); ?>
	  <input type="text" name="qtxt" id="qtxt" value="<?php echo $namakontak; ?>" placeholder="Cari Customer ..." size="30" readonly style="background-color:#fff;border:1px solid #999; height:16px;" onclick="NewWindow('index.php?component=kontak&amp;task=list&amp;ref=Customer&amp;open=window','name','825','450','yes');return false" /><script>window.getDetailCustomer('<?php echo $row_edit['kontak']; ?>');window.setKeterangan('<?php echo $row_edit['kontak']; ?>')</script></td>
      <td align="right" valign="top">Pembayaran :</td>
      <td valign="top"><label>
        <select name="pembayaran" id="pembayaran">
          <option value="" <?php if (!(strcmp("", $row_edit['pbayar']))) {echo "selected=\"selected\"";} ?>>Periode Pembayaran</option>
          <option value="1" <?php if (!(strcmp(1, $row_edit['pbayar']))) {echo "selected=\"selected\"";} ?>>/ Bulan</option>
          <option value="2" <?php if (!(strcmp(2, $row_edit['pbayar']))) {echo "selected=\"selected\"";} ?>>/ 2 Bulan</option>
          <option value="3" <?php if (!(strcmp(3, $row_edit['pbayar']))) {echo "selected=\"selected\"";} ?>>/ 3 Bulan</option>
          <option value="6" <?php if (!(strcmp(6, $row_edit['pbayar']))) {echo "selected=\"selected\"";} ?>>/ 6 Bulan</option>
          <option value="12" <?php if (!(strcmp(12, $row_edit['pbayar']))) {echo "selected=\"selected\"";} ?>>/ Tahun</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td align="right" valign="top">Periode Kontrak :</td>
      <td valign="top"><label>
        <input name="awal" type="text" class="calendar" id="awal" value="<?php echo $row_edit['mulai'];?>" size="20" maxlength="15"/>
      </label>
        -
        <label id="divAkhirKontrak">
          <input name="akhir" type="text" id="akhir" value="<?php echo $row_edit['sampai'];?>" size="20" maxlength="15" readonly="readonly"/>
      </label></td>
      <td align="right" valign="top">Keterangan :</td>
      <td valign="top"><div id="divKeterangan">
        <textarea name="keterangan" cols="40" rows="2" id="keterangan"><?php echo $row_edit['info'];?></textarea>
        <input name="cid" type="hidden" id="cid" value=""/>
      </div></td>
    </tr>
    <tr>
		<td colspan="4" align="right">
			<?php
			unset($_SESSION['kendaraan']);
			unset($_SESSION['txtkendaraan']);
			unset($_SESSION['hargasatuan']);
			unset($_SESSION['diskon']);
			unset($_SESSION['transaksi']);
			unset($_SESSION['subtotal']);
			unset($_SESSION['pajak']);
			unset($_SESSION['grandtotal']);
			
			mysql_select_db($database_con_gl, $con_gl);
			$query = mysql_query("select kontrak, kendaraan, harga, diskon, total, transaksi from gl_dkontrak where kontrak = '$row_edit[no]'") or die (mysql_error());
			$i=0;
			while(list($kontrak, $kendaraan, $harga, $diskon, $total, $transaksi) = mysql_fetch_row($query)){
				$_SESSION['kendaraan'][$i] 		= $kendaraan;
				list($tipe) = mysql_fetch_row(mysql_query("select B.tipekendaraan from gl_masterkendaraan as A inner join gl_tipekendaraan as B on (A.tipekendaraan = B.id) where A.nopolisi = '$kendaraan'"));
				$_SESSION['txtkendaraan'][$i] 	= $tipe;
				$_SESSION['hargasatuan'][$i] 	= $harga;
				$_SESSION['diskon'][$i]			= $diskon;
				$_SESSION['transaksi'][$i]		= $transaksi;
				$_SESSION['subtotal'] += $harga-($harga*$diskon/100);
				$i++;
			}
			$_POST['pajak'] = $row_edit['pajak'];
			$_SESSION['pajak'] = $_SESSION['subtotal']*($row_edit['pajak']/100);
			$_POST['biayalain'] = $row_edit['biaya'];
			$_SESSION['grandtotal'] = $row_edit['total'];
			?>
			<div id="ajaxDiv"><?php include "detail.php"; ?></div>
		</td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td colspan="3" align="right"><input name="page" type="hidden" id="page" value="jual" />
        <input name="jenis" type="hidden" id="jenis" value="7" />
        <label>
          <input name="Save" type="submit" id="Save" value="Hapus Kontrak" />
        </label></td>
    </tr>
  </table>
</form>